<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="Tom的博客">
    <meta name="keyword"  content="">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        Linux使用命令行发邮件 - Tom的博客
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/css/aircloud.css">

    
<link rel="stylesheet" href="/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 5.4.0"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 间歇性壮志凌云, 持续性混吃等死 </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>Tom</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archives">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AEPOP3-SMTP%E6%9C%8D%E5%8A%A1"><span class="toc-text">设置POP3&#x2F;SMTP服务</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%89%E8%A3%85%E5%92%8C%E9%85%8D%E7%BD%AE%E8%BD%AF%E4%BB%B6"><span class="toc-text">安装和配置软件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8%E8%BD%AF%E4%BB%B6"><span class="toc-text">使用软件</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> 间歇性壮志凌云, 持续性混吃等死 </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        Linux使用命令行发邮件
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2019-08-09 21:08:01</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#linux" title="linux">linux</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <p>虽然在Linux下有很多邮件软件，但是有的时候我们还是需要在命令行下发送邮件，网上的教程也有很多，但是我尝试了很多，大多数都失败了，记录一下我为数不多的成功经历。</p>
<h4 id="设置POP3-SMTP服务"><a href="#设置POP3-SMTP服务" class="headerlink" title="设置POP3/SMTP服务"></a>设置POP3/SMTP服务</h4><p>以163邮箱为例，先进入你的163邮箱，在设置下面的POP3/SMTP/IMAP下设置POP3/SMTP服务。</p>
<blockquote>
<p>开启服务之后，163邮箱会让你设置授权码，一定要记住这个授权码，下面讲到的邮箱配置文件中的密码是这个授权码，而不是你登陆邮箱的密码!</p>
</blockquote>
<p><img src="../img/2019-08-09-21-14-59.png" alt=""></p>
<h4 id="安装和配置软件"><a href="#安装和配置软件" class="headerlink" title="安装和配置软件"></a>安装和配置软件</h4><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">sudo apt instmail=&quot;/usr/bin/msmtp&quot;</span><br><span class="line">set use_from=yes</span><br><span class="line">set realname=&quot;无所谓&quot;</span><br><span class="line">set from=tom@163.com</span><br><span class="line">set envelope_from=yes</span><br></pre></td></tr></table></figure>
<p>然后新建日志文件<code>touch ~/.msmtp.log</code><br>新建msmtp配置文件<code>vim ~/.msmtprc</code>，写入以下内容</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">account	default</span><br><span class="line">host	smtp.163.com</span><br><span class="line">user	tom</span><br><span class="line">from	tom@163.com</span><br><span class="line">password	xxxxxxx</span><br><span class="line">auth	login</span><br><span class="line">tls	off</span><br><span class="line">logfile	~/.msmtp.log</span><br></pre></td></tr></table></figure>
<blockquote>
<p>user不能随便填写，一定要是邮箱@之前的字符</p>
</blockquote>
<p>由于密码是明文，所以修改文件的权限，只允许自己可以访问该文件 <code>chmod 600 ~/.msmtprc</code><br>配置好之后可以输入<code>msmtp --host=smtp.163.com --serverinfo</code>命令，进行测试，输出以下结果</p>
<blockquote>
<p>SMTP server at smtp.163.com (m12-16.163.com [220.181.12.16]), port 25:<br>    163.com Anti-spam GT for Coremail System (163com[20141201])<br>Capabilities:<br>    PIPELINING:<br>        Support for command grouping for faster transmission<br>    STARTTLS:<br>        Support for TLS encryption via the STARTTLS command<br>    AUTH:<br>        Supported authentication methods:<br>        PLAIN LOGIN<br>This server might advertise more or other capabilities when TLS is active.</p>
</blockquote>
<h4 id="使用软件"><a href="#使用软件" class="headerlink" title="使用软件"></a>使用软件</h4><p>然后就可以开始使用命令行发送邮件了。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">echo &quot;hello world&quot; | mutt -s &quot;title&quot; 目标邮箱</span><br><span class="line">mutt -s &quot;title&quot; -a 附件 -- 目标邮箱 &lt; 邮件正文</span><br></pre></td></tr></table></figure>
        
            <div class="donate-container">
    <div class="donate-button">
        <button id="donate-button">赞赏</button>
    </div>
    <div class="donate-img-container hide" id="donate-img-container">
        <img id="donate-img" src="" data-src="/img/donate.jpg">
        <p> 感谢鼓励 </p>
    </div>
</div>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        

        

    </ul>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




</html>
